﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class LaneDA : ILaneDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string DOOR = "@DOOR";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string LANE_ID = "@LANE_ID";
        private string LANE_SEQ = "@LANE_SEQ";
        private string LANE_TYPE = "@LANE_TYPE";
        private string OH_TYPE = "@OH_TYPE";
        private string OWNER_ID = "@OWNER_ID";
        private string SKU_ID = "@SKU_ID";
        private string SQL_CHECK_LANE_ID_UNIQUE = " SELECT COUNT(1) FROM @_@LANE WHERE LANE_ID = @LANE_ID ";
        private string SQL_DELETE_LANE = " DELETE FROM @_@LANE WHERE LANE_ID = @LANE_ID ";
        private string SQL_INSERT_LANE = " INSERT INTO @_@LANE ( WH_ID, LANE_ID, LANE_TYPE, OWNER_ID, SKU_ID, DOOR, STATUS, LANE_SEQ, OH_TYPE, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @LANE_ID, @LANE_TYPE, @OWNER_ID, @SKU_ID, @DOOR, @STATUS, @LANE_SEQ, @OH_TYPE, @IS_ACTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_LANE = " SELECT  ROWID, WH_ID, LANE_ID, LANE_TYPE, OWNER_ID, SKU_ID, DOOR, STATUS, LANE_SEQ, OH_TYPE, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@LANE ";
        private string SQL_UPDATE_LANE = " UPDATE @_@LANE SET WH_ID = @WH_ID, LANE_TYPE = @LANE_TYPE, OWNER_ID = @OWNER_ID, SKU_ID = @SKU_ID, DOOR = @DOOR, STATUS = @STATUS, LANE_SEQ = @LANE_SEQ, OH_TYPE = @OH_TYPE, IS_ACTIVE = @IS_ACTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE LANE_ID = @LANE_ID  ";
        private string STATUS = "@STATUS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckLaneIDUnique(string laneID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANE_ID", laneID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_LANE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckLaneIDUnique(DataBase dataBase, DbTransaction tran, string laneID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANE_ID", laneID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_LANE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteLane(List<string> laneID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < laneID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANE_ID", laneID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_LANE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteLane(DataBase dataBase, DbTransaction tran, List<string> laneID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < laneID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANE_ID", laneID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_LANE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllLane(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LANE.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllLane(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LANE.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public LaneInfo GetLaneByID(string laneID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LANE.Replace("@_@", whLoginID) + " WHERE LANE_ID = @LANE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANE_ID", laneID) };
            LaneInfo laneInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    laneInfo = new LaneInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["LANE_ID"].ToString(), reader["LANE_TYPE"].ToString(), reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["DOOR"].ToString(), reader["STATUS"].ToString(), (reader["LANE_SEQ"].ToString() != "") ? int.Parse(reader["LANE_SEQ"].ToString()) : 0, reader["OH_TYPE"].ToString(), reader["IS_ACTIVE"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return laneInfo;
        }

        public LaneInfo GetLaneByID(DataBase dataBase, DbTransaction tran, string laneID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LANE.Replace("@_@", whLoginID) + " WHERE LANE_ID = @LANE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANE_ID", laneID) };
            LaneInfo laneInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                laneInfo = new LaneInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["LANE_ID"].ToString(), reader["LANE_TYPE"].ToString(), reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["DOOR"].ToString(), reader["STATUS"].ToString(), (reader["LANE_SEQ"].ToString() != "") ? int.Parse(reader["LANE_SEQ"].ToString()) : 0, reader["OH_TYPE"].ToString(), reader["IS_ACTIVE"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return laneInfo;
        }

        public DataSet GetLaneByQueryList(List<string> sqlWhere, LaneQueryEntity laneQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_LANE.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!laneQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, laneQuery.CurrentPage, laneQuery.PageSize, laneQuery.SortField, laneQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetLaneByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, LaneQueryEntity laneQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_LANE.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, laneQuery.CurrentPage, laneQuery.PageSize, laneQuery.SortField, laneQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertLane(LaneInfo laneInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    laneInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_Lane_Parameters(laneInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_LANE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int InsertLane(DataBase dataBase, DbTransaction tran, LaneInfo laneInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    laneInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_Lane_Parameters(laneInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_LANE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_Lane_Parameters(LaneInfo laneInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.LANE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.LANE_TYPE, SqlDbType.VarChar, 20), new SqlParameter(this.OWNER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.SKU_ID, SqlDbType.VarChar, 50), new SqlParameter(this.DOOR, SqlDbType.VarChar, 20), new SqlParameter(this.STATUS, SqlDbType.VarChar, 20), new SqlParameter(this.LANE_SEQ, SqlDbType.Int, 4), new SqlParameter(this.OH_TYPE, SqlDbType.VarChar, 20), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(laneInfo.WhID))
            {
                paramArray[0].Value = laneInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = laneInfo.LaneID;
            paramArray[2].Value = laneInfo.LaneType;
            if (!string.IsNullOrEmpty(laneInfo.OwnerID))
            {
                paramArray[3].Value = laneInfo.OwnerID;
            }
            else
            {
                paramArray[3].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(laneInfo.SkuID))
            {
                paramArray[4].Value = laneInfo.SkuID;
            }
            else
            {
                paramArray[4].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(laneInfo.Door))
            {
                paramArray[5].Value = laneInfo.Door;
            }
            else
            {
                paramArray[5].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(laneInfo.Status))
            {
                paramArray[6].Value = laneInfo.Status;
            }
            else
            {
                paramArray[6].Value = DBNull.Value;
            }
            paramArray[7].Value = laneInfo.LaneSeq;
            if (!string.IsNullOrEmpty(laneInfo.OhType))
            {
                paramArray[8].Value = laneInfo.OhType;
            }
            else
            {
                paramArray[8].Value = DBNull.Value;
            }
            paramArray[9].Value = laneInfo.IsActive;
            paramArray[10].Value = laneInfo.CreatedBy;
            paramArray[11].Value = laneInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateLane(LaneInfo laneInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    laneInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_Lane_Parameters(laneInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_LANE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateLane(DataBase dataBase, DbTransaction tran, LaneInfo laneInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    laneInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_Lane_Parameters(laneInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_LANE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

